unit UCadModeloEtapa;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UModeloFilha, DB, DBTables, StdCtrls, Buttons, Mask, DBCtrls,
  ExtCtrls, Grids, DBGrids, UDptoUteis;

type
  TFCadModeloEtapa = class(TFModeloFilha)
    qFilhaMODETPDESCRICAO: TStringField;
    qFilhaMODETPID: TIntegerField;
    qFilhaMODID: TIntegerField;
    lbl1: TLabel;
    dbedt1: TDBEdit;
    ds1: TDataSource;
    lbl2: TLabel;
    dbedt2: TDBEdit;
    lbl3: TLabel;
    dbedt3: TDBEdit;
    Edit1: TEdit;
    dsMODELOETAPAMATERIAL: TDataSource;
    QMODELOETAPAMATERIAL: TQuery;
    dbgrdMODELOETAPAMATERIAL: TDBGrid;
    Ppnl12: TPanel;
    QMODELOETAPAMATERIALMODETPMATQUANTIDADE: TFloatField;
    QMODELOETAPAMATERIALIDMATERIAL: TIntegerField;
    QMODELOETAPAMATERIALMODETPID: TIntegerField;
    QMODELOETAPAMATERIALMODID: TIntegerField;
    QMODELOETAPAMATERIALMATDESCRICAO: TStringField;
    procedure dbgrdMODELOETAPAMATERIALKeyDown(Sender: TObject;
      var Key: Word; Shift: TShiftState);
    procedure qFilhaAfterScroll(DataSet: TDataSet);
    procedure dbgrdMODELOETAPAMATERIALDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadModeloEtapa: TFCadModeloEtapa;

implementation

uses UCadModeloEtapaMaterial;

{$R *.dfm}

procedure TFCadModeloEtapa.dbgrdMODELOETAPAMATERIALKeyDown(Sender: TObject;  var Key: Word; Shift: TShiftState);
   var x,i:integer;
      FormularioFilho :TFCadModeloEtapaMaterial;
      vSql:String;
begin
  inherited;
   if Ord(Key) = 45 then
  Begin

      IF qFilha.State = dsbrowse then
      begin
        FCadModeloEtapaMaterial := TFCadModeloEtapaMaterial.create(nil);
        //
        FormularioFilho := FCadModeloEtapaMaterial;

        FormularioFilho.qFilha.close;
        FormularioFilho.qFilha.open;
        FormularioFilho.qFilha.append;
        //
        for i:= 0 to qFilha.FieldCount -1 do
        begin
          for x:= 0 to FormularioFilho.qFilha.FieldCount-1 do
          begin
            if qFilha.Fields[i].FieldName = FormularioFilho.qFilha.Fields[x].FieldName then
             FormularioFilho.qFilha.FieldByName(FormularioFilho.qFilha.Fields[x].FieldName).AsVariant := qFilha.fieldByname(qFilha.Fields[i].FieldName).AsVariant;

          end;
        end;

          vSQL := 'select MODDESCRICAO from MODELO where MODID = ' + QuotedStr(qFilha.fieldByName('MODID').AsString) ;
          qGenerica.Close;
          qGenerica.SQL.Clear;
          qGenerica.SQL.Add(vSQL);
          qgenerica.Open;
          FormularioFilho.EdModDescricao.Text := qGenerica.fieldByName('MODDESCRICAO').AsString;

          //

          vSQL := 'select MODETPDESCRICAO from MODELOETAPA where MODETPID = ' + QuotedStr(qFilha.fieldByName('MODETPID').AsString) ;
          qGenerica.Close;
          qGenerica.SQL.Clear;
          qGenerica.SQL.Add(vSQL);
          qgenerica.Open;
          FormularioFilho.EdModEtapaDescricao.Text := qGenerica.fieldByName('MODETPDESCRICAO').AsString;


        FormularioFilho.Showmodal;
        //
          if (qFilha.fieldByName('MODID').AsString <> '') and (qFilha.fieldByName('MODETPID').AsString <> '') then
          Begin
            vSql := 'SELECT MEM.*, M.MATDESCRICAO FROM MODELOETAPAMATERIAL MEM, MATERIAIS M'
                    + ' WHERE MEM.MODID = ' + QuotedStr(qFilha.fieldByName('MODID').AsString)
                    + ' AND MEM.MODETPID = ' + QuotedStr(qFilha.fieldByName('MODETPID').AsString)
                    + ' AND M.IDMATERIAL = MEM.IDMATERIAL';

            QMODELOETAPAMATERIAL.Close;
            QMODELOETAPAMATERIAL.SQL.Clear;
            QMODELOETAPAMATERIAL.SQL.Add(vSQL);
            QMODELOETAPAMATERIAL.Open;
          end;
      end;
  end;
end;

procedure TFCadModeloEtapa.qFilhaAfterScroll(DataSet: TDataSet);
var vSQL:string;
begin
  inherited;
  if (qFilha.fieldByName('MODID').AsString <> '') and (qFilha.fieldByName('MODETPID').AsString <> '') then
  Begin
    vSql := 'SELECT MEM.*, M.MATDESCRICAO FROM MODELOETAPAMATERIAL MEM, MATERIAIS M'
            + ' WHERE MEM.MODID = ' + QuotedStr(qFilha.fieldByName('MODID').AsString)
            + ' AND MEM.MODETPID = ' + QuotedStr(qFilha.fieldByName('MODETPID').AsString)
            + ' AND M.IDMATERIAL = MEM.IDMATERIAL';

    QMODELOETAPAMATERIAL.Close;
    QMODELOETAPAMATERIAL.SQL.Clear;
    QMODELOETAPAMATERIAL.SQL.Add(vSQL);
    QMODELOETAPAMATERIAL.Open;
  end;
end;



procedure TFCadModeloEtapa.dbgrdMODELOETAPAMATERIALDblClick(
  Sender: TObject);
begin
  inherited;
       FCadModeloEtapaMaterial := tFCadModeloEtapaMaterial.create(nil);
      //
      FCadModeloEtapaMaterial.qFilha.close;
      FCadModeloEtapaMaterial.qFilha.sql.Clear;


    vSql := 'SELECT * FROM MODELOETAPAMATERIAL'
          + ' WHERE MODID = ' + quotedStr(QMODELOETAPAMATERIAL.FieldByName('MODID').ASSTRING)
          + ' and MODETPID = '  + quotedStr(QMODELOETAPAMATERIAL.FieldByName('MODETPID').ASSTRING)
          + ' and IDMATERIAL = ' + quotedStr(QMODELOETAPAMATERIAL.FieldByName('IDMATERIAL').ASSTRING);

      FCadModeloEtapaMaterial.qFilha.sql.Add(vSql);
      FCadModeloEtapaMaterial.qFilha.open;

      // CARREGA DESCRICOES

      // BUSCA DESCRICAO DO MODELO
      vSQL := 'select MODDESCRICAO from MODELO where MODID = ' + QuotedStr(QMODELOETAPAMATERIAL.fieldByName('MODID').AsString) ;
      qGenerica.Close;
      qGenerica.SQL.Clear;
      qGenerica.SQL.Add(vSQL);
      qgenerica.Open;
      FCadModeloEtapaMaterial.EdModDescricao.Text := qGenerica.fieldByName('MODDESCRICAO').AsString;

      // BUSCA DESCRICAO DA ETAPA
      vSQL := 'select MODETPDESCRICAO from MODELOETAPA where MODETPID = ' + QuotedStr(QMODELOETAPAMATERIAL.fieldByName('MODETPID').AsString) ;
      qGenerica.Close;
      qGenerica.SQL.Clear;
      qGenerica.SQL.Add(vSQL);
      qgenerica.Open;
      FCadModeloEtapaMaterial.EdModEtapaDescricao.Text := qGenerica.fieldByName('MODETPDESCRICAO').AsString;

      // BUSCA DESCRICAO DO PRODUTO
      vSQL := 'select MATDESCRICAO from MATERIAIS where IDMATERIAL = ' + QuotedStr(QMODELOETAPAMATERIAL.fieldByName('IDMATERIAL').AsString) ;
      qGenerica.Close;
      qGenerica.SQL.Clear;
      qGenerica.SQL.Add(vSQL);
      qgenerica.Open;
      FCadModeloEtapaMaterial.EdMatDescricao.Text := qGenerica.fieldByName('MATDESCRICAO').AsString;


      //

      FCadModeloEtapaMaterial.Showmodal;
      QMODELOETAPAMATERIAL.Close;
      QMODELOETAPAMATERIAL.Open;

end;

procedure TFCadModeloEtapa.FormCreate(Sender: TObject);
begin
  inherited;
  FINAL := FALSE;
end;

end.
